<?php

class Generate_data extends CI_Controller {

  var $generator;

  public function __construct() {
    parent::__construct();
//	redirect('admin');
    $this->load->library('loremIpsumGenerator');
    $this->generator = new LoremIpsumGenerator;
  }

  public function create_dir() {
    $promotions = $this->promotion_model->get_all();
    $old = umask(0);
    foreach ($promotions as $promotion) {
      @mkdir('./uploads/promotion/' . $promotion['id'], 0777);
      for ($i = 1; $i <= 9; $i++) {
        $j = rand(1, 47);
        copy('./uploads/promotion/1 (' . $j . ').jpg', './uploads/promotion/' . $promotion['id'] . '/' . $j . '.jpg');
        $this->db->insert(DB_TABLE_PROMOTION_MEDIA, array(
            'promotion_id' => $promotion['id'],
            'path' => $j . '.jpg',
            'original_name' => 'image_' . $j . '.png',
            'is_main' => rand(0, 1),
            'type' => 'image'
        ));
      }
    }
    umask($old);
  }

  public function category() {
    $num_cat = 10;
    for ($i = 0; $i < $num_cat; $i++) {
      $file = md5($this->auth->generateActiveCode(50)) . '.png';
      copy('./uploads/temp/a (' . rand(1, 555) . ').png', './uploads/category/' . $file);
      $this->db->insert(DB_TABLE_CATEGORY, array(
          'name' => $this->generator->getContent(rand(2, 4), 'plain', false),
          'date_create' => time(),
          'parent_id' => rand(0, $i),
          'order' => $i,
          'avatar' => $file
      ));
    }
  }

  public function country_city() {
    $num_co = 10;
    $num_ci = 5;
    for ($i = 0; $i < $num_co; $i++) {
      $file = md5($this->auth->generateActiveCode(50)) . '.png';
      copy('./uploads/flag/a (' . ($i + 1) . ').png', './uploads/country/' . $file);
      $this->db->insert(DB_TABLE_COUNTRY, array(
          'name' => trim($this->generator->getContent(rand(2, 4), 'plain', false)),
          'code' => trim($this->generator->getContent(1, 'plain', false)),
          'lat' => rand(26, 47) . '.040182',
          'lng' => rand(-123, -69) . '.773437',
          'flag_path' => $file
      ));
      $id = $this->db->insert_id();
      for ($j = 0; $j < $num_ci; $j++) {
        $this->db->insert(DB_TABLE_CITY, array(
            'name' => $this->generator->getContent(rand(2, 4), 'plain', false),
            'country_id' => $id,
            'lat' => rand(26, 47) . '.040182',
            'lng' => rand(-123, -69) . '.773437'
        ));
      }
    }
  }

  public function provider() {
    $num_provider = 10;
    $num_co = $this->db->count_all_results(DB_TABLE_COUNTRY);
    $num_ci = $this->db->count_all_results(DB_TABLE_CITY);
    for ($i = 0; $i < $num_provider; $i++) {
      $this->db->insert(DB_TABLE_USER, array(
          'username' => 'provider_' . trim($this->generator->getContent(1, 'plain', false)) . $i,
          'password' => md5('123123'),
          'created_on' => time(),
          'display_name' => $this->generator->getContent(rand(3, 4), 'plain', false),
          'group_id' => 2,
      ));
      $id = $this->db->insert_id();
      $file = md5($this->auth->generateActiveCode(50)) . '.png';
      copy('./uploads/temp/a (' . rand(1, 555) . ').png', './uploads/provider_user/' . $file);
      $this->db->insert(DB_TABLE_PROVIDER_USER, array(
          'user_id' => $id,
          'email' => trim($this->generator->getContent(1, 'plain', false)) . $i . '.vn@gmail.com',
          'phone' => '0' . $this->auth->generateNumberic(9),
          'address' => $this->generator->getContent(rand(3, 4), 'plain', false),
          'date_active' => time(),
          'date_update' => time(),
          'status' => rand(-1, 1),
          'country_id' => rand(1, $num_co),
          'city_id' => rand(1, $num_ci),
          'company_name' => $this->generator->getContent(rand(3, 4), 'plain', false),
          'home_page' => 'http://' . trim($this->generator->getContent(1, 'plain', false)) . '.com',
          'fax' => '0' . $this->auth->generateNumberic(9),
          'avatar' => $file
      ));
    }
  }

  public function end_user() {
    $num_end_user = 100;
    $num_co = $this->db->count_all_results(DB_TABLE_COUNTRY);
    $num_ci = $this->db->count_all_results(DB_TABLE_CITY);
    for ($i = 0; $i < $num_end_user; $i++) {
      $this->db->insert(DB_TABLE_USER, array(
          'username' => 'end_user_' . trim($this->generator->getContent(1, 'plain', false)),
          'password' => md5('123123'),
          'created_on' => time(),
          'display_name' => $this->generator->getContent(rand(3, 4), 'plain', false),
          'group_id' => 3,
      ));
      $id = $this->db->insert_id();
      $file = md5($this->auth->generateActiveCode(50)) . '.png';
      copy('./uploads/temp/a (' . rand(1, 555) . ').png', './uploads/end_user/' . $file);
      $this->db->insert(DB_TABLE_END_USER, array(
          'user_id' => $id,
          'email' => trim($this->generator->getContent(1, 'plain', false)) . '.vn@gmail.com',
          'phone' => '0' . $this->auth->generateNumberic(9),
          'address' => $this->generator->getContent(rand(3, 4), 'plain', false),
          'date_active' => time(),
          'date_update' => time(),
          'last_login' => 0,
          'status' => rand(-1, 1),
          'country_id' => rand(1, $num_co),
          'city_id' => rand(1, $num_ci),
          'avatar' => $file
      ));
    }
  }

  public function promotion() {
    set_time_limit(-1);
    $num_provider = $this->db->count_all_results(DB_TABLE_PROVIDER_USER);
    $num_end = $this->db->count_all_results(DB_TABLE_END_USER);
    $num_co = $this->db->count_all_results(DB_TABLE_COUNTRY);
    $num_ci = $this->db->count_all_results(DB_TABLE_CITY);
    $num_cat = $this->db->count_all_results(DB_TABLE_CATEGORY);
    $num_promotion = 10;
    $num_picture = 5;
    for ($i = 1; $i <= $num_provider; $i++) {
      for ($j = 0; $j < $num_promotion; $j++) {
        $temp = rand(100, 1000);
        $this->db->insert(DB_TABLE_PROMOTION, array(
            'title' => $this->generator->getContent(8, 'plain', false),
            'description' => $this->generator->getContent(1000, 'plain', false),
            'provider_user_id' => rand(1, $num_provider),
            'start_date' => strtotime('-1 year'),
            'end_date' => rand(0, 1) ? strtotime('+2 year') : time(),
            'date_create' => time(),
            'date_update' => time(),
            'date_publish' => time(),
            'max_per_person' => rand(1, 5),
            'max_download' => $temp,
            'remain' => $temp,
            'lat' => rand(26, 47) . '.040182',
            'lng' => rand(-123, -69) . '.773437',
            'rate_point' => 0,
            'is_hot' => rand(0, 1),
            'status' => rand(-1, 1),
            'country_id' => rand(1, $num_co),
            'city_id' => rand(1, $num_ci),
            'code' => md5(time() . uniqid() . time())
        ));
        $id = $this->db->insert_id();
        mkdir('./uploads/promotion/' . $id);
        for ($k = 0; $k < $num_picture; $k++) {
          $file = md5($this->auth->generateActiveCode(50)) . '.png';
          copy('./uploads/temp/a (' . rand(1, 555) . ').png', './uploads/promotion/' . $id . '/' . $file);
          $this->db->insert(DB_TABLE_PROMOTION_MEDIA, array(
              'promotion_id' => $id,
              'path' => $file,
              'is_main' => rand(0, 1),
              'type' => 'image'
          ));
        }
        $cat = rand(1, 5);
        for ($k = 0; $k < $cat; $k++) {
          $this->db->insert(DB_TABLE_CATEGORY_PROMOTION, array(
              'promotion_id' => $id,
              'cat_id' => rand(1, $num_cat),
              'date_create' => time()
          ));
        }

        $view = rand(1, 100);
        for ($k = 0; $k < $view; $k++) {
          $this->db->insert(DB_TABLE_PROMOTION_VIEW, array(
              'promotion_id' => $id,
              'end_user_id' => rand(1, $num_end),
              'date_create' => time(),
              'ip' => rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255)
          ));
        }
        $favorite = 10;
        for ($k = 0; $k < $favorite; $k++) {
          $this->db->insert(DB_TABLE_PROMOTION_FAVORITE, array(
              'promotion_id' => $id,
              'end_user_id' => rand(1, $num_end),
              'date_create' => time()
          ));
        }

        $download = 10;
        for ($k = 0; $k < $download; $k++) {
          $this->db->insert(DB_TABLE_PROMOTION_DOWNLOAD, array(
              'promotion_id' => $id,
              'end_user_id' => rand(1, $num_end),
              'date_create' => time(),
              'code' => md5(time() . uniqid() . time()),
              'status' => PROMOTION_DOWNLOAD_NEW,
              'num_get' => 1
          ));
        }

        $relate = 5;
        for ($k = 0; $k < $relate; $k++) {
          $this->db->insert(DB_TABLE_PROMOTION_RELATE, array(
              'promotion1_id' => $id,
              'promotion1_id' => max(1, rand(1, $id - 1))
          ));
        }
        $rating = rand(1, 100);
        for ($k = 0; $k < $view; $k++) {
          $this->db->insert(DB_TABLE_PROMOTION_RATING, array(
              'promotion_id' => $id,
              'end_user_id' => rand(1, $num_end),
              'date_rate' => time(),
              'point' => rand(0, 10),
              'ip' => rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255) . '.' . rand(1, 255)
          ));
        }
      }
    }
  }

  public function email_template() {
    $num_temp = 10;
    for ($i = 0; $i < $num_temp; $i++) {
      for ($j = 0; $j <= $this->db->count_all_results(DB_TABLE_USER); $j++) {
        $this->db->insert(DB_TABLE_EMAIL_TEMPLATE, array(
            'name' => $this->generator->getContent(rand(2, 4), 'plain', false),
            'user_id' => $j,
            'subject' => $this->generator->getContent(rand(10, 20), 'plain', false),
            'content' => $this->generator->getContent(200, 'plain', false),
            'description' => $this->generator->getContent(20, 'plain', false)
        ));
      }
    }
  }

  public function generate_all() {
    set_time_limit(-1);
    $this->category();
    $this->country_city();
    $this->end_user();
    $this->provider();
    $this->email_template();
    $this->promotion();
  }

}